package com.koron.dynamic.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import com.koron.bean.app.AppBean;
import com.koron.bean.app.ExternalDb;
import com.koron.dynamic.koronDataSource.KrDataSourceUtils;
import com.koron.dynamic.mapper.AppDataSourceMapper;
import com.koron.dynamic.service.KrDataSourceService;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName KrDataSourceServiceImpl
 * @description
 * @Author zhouj
 * @Date 2021/7/19 17:39
 */
@Slf4j
@Service
public class KrDataSourceServiceImpl implements KrDataSourceService {

    @Override
    public void registryMultDb(List<String> markList) {

        List<AppBean> appBeanList = ADOConnection.runTask(factory -> {
            AppDataSourceMapper mapper = factory.getMapper(AppDataSourceMapper.class);
            //  租户数据源
            List<AppBean> appbeanlist = mapper.findDataSourceByKeyList(markList, 1);
            //  外部数据源
            List<ExternalDb> externalDbs = mapper.queryByExternalDB(1);
            if (CollUtil.isNotEmpty(externalDbs)) {
                externalDbs.stream().forEach(e -> {
                    AppBean convert = Convert.convert(AppBean.class, e);
                    appbeanlist.add(convert);
                });
            }
            return appbeanlist;

        }, List.class);

        if (CollectionUtil.isEmpty(appBeanList)) {
            return;
        }
        appBeanList.forEach(KrDataSourceUtils::registDb);
    }

}

